//
//  CZQEidtResumeViewController.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/14.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQEidtResumeViewController.h"
#import "CZQJobTableViewCell.h"
#import "CZQJobInfoTableViewCell.h"
#import "CZQphotoShowTableViewCell.h"
#import "CZQCerTableViewCell.h"
#import "CZQAddResumeViewController.h"

@interface CZQEidtResumeViewController () <UITableViewDataSource,UITableViewDelegate>
/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;
@end

@implementation CZQEidtResumeViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self handleData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *sureBtn = [[UIButton alloc] init];
    [sureBtn setTitle:@"添加 +"];
    [sureBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
    sureBtn.titleLabel.font = MidBigFont;
    sureBtn.backgroundColor = ButtonBackGroundColor;
    [self.view addSubview:sureBtn];
    
    [sureBtn addTarget:self action:@selector(add)];
    
    [sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.view).offset(-20);
        
        make.left.mas_equalTo(self.view).offset(20);
        
        make.right.mas_equalTo(self.view).offset(-20);
        
        make.height.mas_equalTo(50);
    }];
    
    self.view.backgroundColor = BackGroundColor;
    
    [self handleData];
    
}

-(void)handleData
{
    [self.dataArr removeAllObjects];
    
    if (self.section == 4) {
        //工作经历
        self.dataArr = [NSMutableArray arrayWithArray:self.resumeModel._WorkExp];
    }else if (self.section == 5)
    {
        //获得证书
        self.dataArr = [NSMutableArray arrayWithArray:self.resumeModel._CertificateExp];
    }
    else if (self.section == 6)
    {
        //参加培训
        self.dataArr = [NSMutableArray arrayWithArray:self.resumeModel._TrainingExp];
    }

    else if (self.section == 7)
    {
        //教育经历
        self.dataArr = [NSMutableArray arrayWithArray:self.resumeModel._EducationExp];
    }

    [self.tableView reloadData];
    
}

#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        
        tableView.estimatedRowHeight = 50;
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.top.mas_equalTo(self.view);
            
            make.bottom.mas_equalTo(self.view).offset(-90);
            
        }];
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerNib:[UINib nibWithNibName:@"CZQJobTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQJobTableViewCell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQJobInfoTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQJobInfoTableViewCell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQphotoShowTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQphotoShowTableViewCell"];
        
        [tableView registerNib:[UINib nibWithNibName:@"CZQCerTableViewCell" bundle:nil]  forCellReuseIdentifier:@"CZQCerTableViewCell"];
        
        
        _tableView = tableView;
    }
    return _tableView;
}



#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     if (self.section == 4){
        //工作经历
        CZQJobInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQJobInfoTableViewCell"];
        
         CZQWorkExpModel *workModel =[CZQWorkExpModel mj_objectWithKeyValues:self.resumeModel._WorkExp[indexPath.row]];
         
         cell.timeLable.text = [NSString stringWithFormat:@"%@至%@",workModel.BeginDate,workModel.EndDate];
         
         cell.addressLable.text = workModel.CompanyName;
         
         cell.jobLable.text = workModel.Position;
         
         cell.infoLable.text = workModel.Intro;
         
        return cell;
    }
    else if (self.section == 5){
        //获得证书
        CZQCerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQCerTableViewCell"];
        
        CZQCertificateExpModel *cerModel =[CZQCertificateExpModel mj_objectWithKeyValues:self.resumeModel._CertificateExp[indexPath.row]];
        
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",cerModel.Pic,mid_image];
        urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        [cell.cerImageView sd_setImageWithURL:[NSURL URLWithString:cerModel.Pic] placeholderImage:[UIImage imageNamed:@"icon_certificate"]];
        
        cell.cerNameLable.text = cerModel.Name;
        
        cell.cerAdreessLable.text = cerModel.Org;
        
        cell.cerTimeLable.text = cerModel.BeginDate;
        
        return cell;
    }
    else if (self.section == 6){
        
        //学习培训
        CZQCerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CZQCerTableViewCell"];
        
        CZQTrainingExpModel *ctrainModel =[CZQTrainingExpModel mj_objectWithKeyValues:self.resumeModel._TrainingExp[indexPath.row]];
        
        [cell.cerImageView sd_setImageWithURL:[NSURL URLWithString:[Helper imageUrlStringToChinese:ctrainModel.Logo imageType:nil]] placeholderImage:PlaceHold];
        
        cell.cerNameLable.text = ctrainModel.Content;
        
        cell.cerAdreessLable.text = ctrainModel.Org;
        
        cell.cerTimeLable.text = ctrainModel.BeginDate;
        
        return cell;
    }
    else if (self.section == 7){
        //教育经历
        CZQJobInfoTableViewCell *studyCell = [tableView dequeueReusableCellWithIdentifier:@"CZQJobInfoTableViewCell"];
        
        CZQEducationExpModel *educationModel = [CZQEducationExpModel mj_objectWithKeyValues:self.resumeModel._EducationExp[indexPath.row]];
        
        studyCell.timeLable.text = [NSString stringWithFormat:@"%@至%@",educationModel.BeginDate,educationModel.EndDate];
        
        studyCell.addressLable.text = educationModel.School;
        
        studyCell.jobLable.text = educationModel.Profession;
        
        studyCell.infoLable.text = educationModel.Degree;
        
        return studyCell;
    }
    UITableViewCell *cell;
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic;
    
    if (self.section == 4){
        //工作经历
 
//        CZQWorkExpModel *workModel =[CZQWorkExpModel mj_objectWithKeyValues:self.resumeModel.WorkExp[indexPath.row]];
        
        dic = self.resumeModel._WorkExp[indexPath.row];
        
    }
    else if (self.section == 5){
        //获得证书
        
//        CZQCertificateExpModel *cerModel =[CZQCertificateExpModel mj_objectWithKeyValues:self.resumeModel.CertificateExp[indexPath.row]];
        dic = self.resumeModel._CertificateExp[indexPath.row];
        
    }
    else if (self.section == 6){
        
        //学习培训
        
//        CZQTrainingExpModel *ctrainModel =[CZQTrainingExpModel mj_objectWithKeyValues:self.resumeModel.TrainingExp[indexPath.row]];
        dic = self.resumeModel._TrainingExp[indexPath.row];
        
        
    }
    else if (self.section == 7){
        //教育经历
        
//        CZQEducationExpModel *educationModel = [CZQEducationExpModel mj_objectWithKeyValues:self.resumeModel.EducationExp[indexPath.row]];
        dic = self.resumeModel._EducationExp[indexPath.row];
        
    }
    
    CZQAddResumeViewController *add = [CZQAddResumeViewController new];
    
    add.section = self.section;
    
    add.resumeModel = self.resumeModel;
    
    add.dic = dic;
    
    add.row = indexPath.row;
    
    
    
    
    add.title = [NSString stringWithFormat:@"编辑%@",self.title];
    
    [self.navigationController pushViewController:add animated:YES];
    
    
}


#pragma mark - 添加

-(void)add
{
    CZQAddResumeViewController *add = [CZQAddResumeViewController new];
    
    add.section = self.section;
    
    add.resumeModel = self.resumeModel;
    
    add.title = [NSString stringWithFormat:@"添加%@",self.title];
    
    [self.navigationController pushViewController:add animated:YES];
    
    if (self.section == 4){
        //工作经历
        
    }
    else if (self.section == 5){
        //获得证书
        
    }
    else if (self.section == 6){
        
        //学习培训
        
    }
    else if (self.section == 7){
        //教育经历
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
